package cn.huihuo.weibo.service;

import cn.huihuo.weibo.bean.boutique.BoutiqueBean;
import cn.huihuo.weibo.bean.boutique.BoutiqueResultBean;
import cn.huihuo.weibo.module.Boutique;
import cn.huihuo.weibo.module.ProdComment;
import cn.huihuo.weibo.module.Product;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dan
 * Created by zhengjunbo on 2015/10/30.
 */
public class BoutiqueService {
    public static final BoutiqueService service = new BoutiqueService();
    public BoutiqueResultBean findBoutique(Integer pageNum,Integer count){
        BoutiqueResultBean boutiqueResultBean = new BoutiqueResultBean();
        boutiqueResultBean.setBoutiqueBeans(new ArrayList<>());

        Page<Boutique> boutiquePage = Boutique.boutique.findBoutiquePage(pageNum, count);
        boutiqueResultBean.setTotalSize(String.valueOf(boutiquePage.getTotalPage()));

        for(Boutique boutique :boutiquePage.getList()){
            BoutiqueBean boutiqueBean = new BoutiqueBean();
            //找出对应product，根据类型再找出最热的评论，根据商品id找出对应的类型的热度关联，并且取第一个转化成id
            Product product = Product.product.findByProductId(String.valueOf(boutique.getInt("prod_id")));
            boutiqueBean.setProdPicUrl(product.getStr("inner_pic_path")==null?product.getStr("out_pic_path"):product.getStr("inner_pic_path"));//外部的
            boutiqueBean.setProdPrice("$" + product.getDouble("price"));
            boutiqueBean.setProdTitle(product.getStr("prod_title"));
            boutiqueBean.setProdUrl("/product/"+product.getInt("prodId"));

            //找出三种类型的数据
            //获取出所有讨论的热度，获取出所有摘抄的热度，获取出所有渠道的热度，关联热度表

            List<ProdComment> commList2 = ProdComment.prodComment.findHotProd(product.getInt("prodId"), "2");//获得评论
            boutiqueBean.setCommNum(commList2.size()+"条讨论");
            if(commList2.size()>0){
                ProdComment prodComment = commList2.get(0);
                boutiqueBean.setCommComment(prodComment.getStr("product_comment"));
                boutiqueBean.setCommName(prodComment.getStr("nickname"));
                boutiqueBean.setCommNameUrl("/user/"+prodComment.getStr("userId"));

            }



            List<ProdComment> commList3 = ProdComment.prodComment.findHotProd(product.getInt("prodId"), "3");//获得摘抄
            boutiqueBean.setExcerptNum(commList3.size() + "个摘抄");
            if(commList3.size()>0){
                ProdComment prodComment = commList3.get(0);
                boutiqueBean.setExtractComment(prodComment.getStr("product_comment"));
                boutiqueBean.setExtractName(prodComment.getStr("nickname"));
                boutiqueBean.setExtractNameUrl("/user/" + prodComment.getStr("userId"));
                boutiqueBean.setExtractUrl(prodComment.getStr("url"));
            }

            List<ProdComment> commList4 = ProdComment.prodComment.findHotProd(product.getInt("prodId"), "4");//获得渠道
            boutiqueBean.setChannelNum(commList4.size() + "个购买渠道");
            if(commList4.size()>0){
                ProdComment prodComment = commList4.get(0);
                boutiqueBean.setChannelComment(prodComment.getStr("product_comment"));
                boutiqueBean.setChannelName(prodComment.getStr("nickname"));
                boutiqueBean.setChannelNameUrl("/user/"+prodComment.getStr("userId"));
                boutiqueBean.setChannelUrl(prodComment.getStr("url"));
            }


            boutiqueResultBean.getBoutiqueBeans().add(boutiqueBean);
        }

        return boutiqueResultBean;
    }
}
